package com.controller
{
	
	import com.model.valueObjects.VoClientMaster;
	import com.service.CustomerMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class CustomerMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var customerMasterService:CustomerMaster;
		
		
		//[PostConstruct]
		public function CustomerMasterController()
		{
			super();	
		}
		
		[EventHandler(event="CustomerMasterEvent.GET_ALL")]
		public function getAllClients():void{
			model.currentAdminTableIndex=4;
			sh.executeServiceCall(customerMasterService.getAllClients(),getAllResult, handleFault );
			
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.clientMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="CustomerMasterEvent.GET_ALL_SUPPLIER")]
		public function getAllSupplier():void{
			sh.executeServiceCall(customerMasterService.getAllSuppliers(),getAllSupplierResult, handleFault );
		}
		private function getAllSupplierResult(event:ResultEvent):void
		{
			model.supplierListDetails=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CustomerMasterEvent.GET_ALL_CUSTOMER")]
		public function getAllCustomer():void{
			sh.executeServiceCall(customerMasterService.getAllCustomer(),getAllCustomerResult, handleFault );
		}
		private function getAllCustomerResult(event:ResultEvent):void
		{
			model.customerListDetails=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CustomerMasterEvent.ADD", properties="customerMastervo")]
		public function add(customerMastervo:VoClientMaster):void
		{
			sh.executeServiceCall(customerMasterService.addNewClient(customerMastervo), addResult, handleFault );	
		}		
		private function addResult(event:ResultEvent):void
		{
			model.clientMasterList=event.result as ArrayCollection;
			
			if(model.saveCloseBtn)model.currentForm=1;

			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Client Added","CLIENT/VENDOR" );
		}
		
		[EventHandler(event="CustomerMasterEvent.UPDATE", properties="customerMastervo")]
		public function updateCompany(customerMastervo:VoClientMaster):void
		{
			sh.executeServiceCall(customerMasterService.editClient(customerMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.clientMasterList=event.result as ArrayCollection;
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ","CLIENT/VENDOR");
		}
		
		[EventHandler(event="CustomerMasterEvent.DELETE", properties="serialnumber")]
		public function deleteR(serialnumber:int):void
		{
			sh.executeServiceCall(customerMasterService.deleteClient(serialnumber), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			model.currentForm=1;
			getAllClients();
			CursorManager.removeBusyCursor();
			Alert.show("Record Deleted ","CLIENT/VENDOR" );
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}